import api from './api';

const examService = {
    /**
     * Sends a description to the backend to generate an exam.
     * @param {string} description - The teacher's requirements for the exam.
     * @returns {Promise<object>} The generated exam data.
     */
    async generateExam(description) {
        try {
            const response = await api.post('/api/exams/generate', { description });
            if (response.data && response.data.status === 0) {
                return response.data.data;
            } else {
                throw new Error(response.data.message || 'Failed to generate exam.');
            }
        } catch (error) {
            const errorMessage = error.response?.data?.message || error.message || 'An error occurred while generating the exam.';
            throw new Error(errorMessage);
        }
    },

    /**
     * Publishes an existing exam.
     * @param {number} examId - The ID of the exam to publish.
     * @param {number} durationInMinutes - The duration of the exam in minutes.
     * @returns {Promise<object>} The updated exam data.
     */
    async publishExam(examId, durationInMinutes) {
        try {
            const response = await api.post(`/api/exams/${examId}/publish`, { durationInMinutes });
            if (response.data && response.data.status === 0) {
                return response.data.data;
            } else {
                throw new Error(response.data.message || 'Failed to publish exam.');
            }
        } catch (error) {
            const errorMessage = error.response?.data?.message || error.message || 'An error occurred while publishing the exam.';
            throw new Error(errorMessage);
        }
    },

    /**
     * 【新方法】获取所有试卷列表
     * @returns {Promise<Array>} 包含所有试卷对象的数组
     */
    async getAllExams() {
        try {
            const response = await api.get('/api/exams');
            if (response.data && response.data.status === 0) {
                return response.data.data;
            } else {
                throw new Error(response.data.message || 'Failed to fetch exams.');
            }
        } catch (error) {
            const errorMessage = error.response?.data?.message || error.message || 'An error occurred while fetching exams.';
            throw new Error(errorMessage);
        }
    },

    /**
     * 【新方法】根据ID获取单份试卷详情
     * @param {number} examId - 试卷ID
     * @returns {Promise<object>} 试卷详情对象
     */
    async getExamById(examId) {
        try {
            const response = await api.get(`/api/exams/${examId}`);
            if (response.data && response.data.status === 0) {
                return response.data.data;
            } else {
                throw new Error(response.data.message || 'Failed to fetch exam details.');
            }
        } catch (error) {
            const errorMessage = error.response?.data?.message || error.message || 'An error occurred while fetching exam details.';
            throw new Error(errorMessage);
        }
    }
};

export default examService;